package com.azt.provider.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.ProjectAnalysis;
import com.azt.api.service.ProjectAnalysisService;
import com.azt.data.utils.Html2ImageUtil;
import com.azt.provider.mapper.ProjectAnalysisMapper;

@Service
@Transactional
public class ProjectAnalysisServiceImpl implements ProjectAnalysisService {
	@Autowired
	private ProjectAnalysisMapper ProjectAnalysisMapper;

	@Override
	public Map<String, List<String>> getProjectPrice(String currentPath,String month) {
		currentPath=currentPath+"/priceImage";
	      File dir = new File(currentPath);  
	        if (!dir.exists()) { 
	        	dir.mkdirs();
	        }  
	     // 先清空priceImage文件夹图片
		File file = new File(currentPath);
		String[] filelist = file.list();
		File temp = null;
		if(filelist!=null){
			for (int i = 0; i < filelist.length; i++) {
				if (currentPath.endsWith(File.separator)) {
					temp = new File(currentPath + filelist[i]);
				} else {
					temp = new File(currentPath + File.separator + filelist[i]);
				}

				if (temp.isFile()) {
					temp.delete();
				}
			}
		}


		// 获取分类排行
		// Map<String, List<Map<String, Object>>> map=new LinkedHashMap<String,
		// List<Map<String, Object>>>();
		Map listUrlMap = new LinkedHashMap();
		//根据月份来生成分类
		//String[] yearOrmonth = month.split("-");获取选择的这个月的最后一天
		//String lastDayOfMonth = CommonUtil.getLastDayOfMonth(Integer.parseInt(yearOrmonth[0]),Integer.parseInt(yearOrmonth[1]));
		List<Map<String, Object>> productCategory = ProjectAnalysisMapper.getProjectCategory(month+"-01");
		
		if (productCategory != null && productCategory.size() > 0) {
			for (Map<String, Object> category : productCategory) {
                //分类URL
				List listUrl=new ArrayList();
				Integer categoryId = (Integer) category.get("id");
				List<Map<String, Object>> productPrices = ProjectAnalysisMapper.getProjectPrice(categoryId,month+"-01");

				if (productPrices != null && productPrices.size() > 0) {
					// 生成图片
					String imagePath = "";
					String imageName = "";
					String thHtml = "<div style='width:750px;'>"
							+ "<table style='width:100%;text-align: center;padding:10px 10px;border-collapse: collapse;border-spacing: 0;font-size:14px;color: #34495e'>"
							+ "<tbody style='border-top: 1px solid #ebebeb;border-bottom: 1px solid #ebebeb;'>"
							+ "<tr style='border: 1px solid #ebebeb;'>"
							+ "<th width='17%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>商品名称</th>"
							+ "<th width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>品牌</th>"
							+ "<th width='20%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>规格型号</th>"
							+ "<th width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>单位</th>"
							+ "<th width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>数量</th>"
							+ "<th width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>单价（元）</th>"
							+ "<th width='15%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>送货区域</th>"
//							+ "<th width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>报价时间</th>"
							+ "</tr>";
					
					Integer pageSize=20;
					String tdHtml ="";
					if (productPrices.size() > pageSize) {

						int size = 0;
						for (int i = 0; i < productPrices.size(); i++) {
							Map<String, Object> productmap = productPrices.get(i);
							tdHtml = tdHtml+"<tr style='border: 1px solid #ebebeb;border-top:none;";
							if(i%2==0){
								tdHtml=tdHtml+"background-color: #f9f9f9;";
							}
							tdHtml =tdHtml+ "'>"
                            +"<td width='17%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("name") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("brand") + "</td>"
							+"<td width='20%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("model") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("unit") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("quantity") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("unitprice") + "</td>"
							+"<td width='15%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("provinceName") + "</td>"
//							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("quoteDate") + "</td>"
							+"</tr>";
							size++;
							
							
							if (size == pageSize || i==productPrices.size()-1) {
								//不是第一张
						      /*if (i > 4) {
									thHtml = "<style>table {border-collapse: collapse;border-spacing: 0;color: #73879c} th td{border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;}</style>"
									+ "<table style='text-align: center;padding:10px 10px'>"
									+ "<tbody style='border-top: 1px solid #ebebeb;border-bottom: 1px solid #ebebeb;'>"
									+ "<tr style='border: 1px solid #ebebeb;border-top:none;'>"
									+ "<th width='15%' ></th>"
									+ "<th width='10%' ></th>"
									+ "<th width='10%' ></th>"
									+ "<th width='10%' ></th>"
									+ "<th width='10%' ></th>"
									+ "<th width='10%' ></th>"
									+ "<th width='15%' ></th>"
									+ "<th width='20%' ></th>"
									+ "</tr>";
								}*/
								imageName = "price_" + categoryId + "_" + i + "_"+new Date().getTime();
								imagePath = currentPath + "/" ;
								String html="";
								html = thHtml + tdHtml + "</tbody></table></div>";
								String ImageUrl =Html2ImageUtil.html2Image(html, imagePath,imageName,"png");
								listUrl.add(ImageUrl);
								tdHtml="";
								size = 0;
								continue;
							}
						}
					} else {
						for (int i = 0; i < productPrices.size(); i++) {
							Map<String, Object> productmap = productPrices.get(i);
							tdHtml = tdHtml+"<tr style='border: 1px solid #ebebeb;";
							if(i%2==0){
								tdHtml=tdHtml+"background-color: #f9f9f9;";
							}
							tdHtml =tdHtml+ "'>"
                            +"<td width='17%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("name") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("brand") + "</td>"
							+"<td width='20%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("model") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("unit") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("quantity") + "</td>"
							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("unitprice") + "</td>"
							+"<td width='15%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("provinceName") + "</td>"
//							+"<td width='12%' style='border-right: 1px solid #ebebeb;text-align: center;padding: 10px 0px;'>" + productmap.get("quoteDate") + "</td>"
							+"</tr>";
						}
						imageName = "price_" + categoryId+"_"+new Date().getTime();
						imagePath = currentPath + "/" ;
						String html="";
						html = thHtml + tdHtml + "</tbody></table></div>";
						String ImageUrl = Html2ImageUtil.html2Image(html, imagePath,imageName,"png");
						listUrl.add(ImageUrl);
					}

					// 存map
					// map.put(category.get("name").toString(), productPrices);
				}
				listUrlMap.put(category.get("name").toString(), listUrl);
			}
		}

		return listUrlMap;
	}

	@Override
	public ProjectAnalysis getProjectAnalysisByMonth(String month) {
		return ProjectAnalysisMapper.getProjectAnalysisByMonth(month);
	}

	@Override
	public void saveOrUpdate(ProjectAnalysis projectAnalysis) {
		if(projectAnalysis!=null){
			//更新
			if(projectAnalysis.getId()!=null){
				ProjectAnalysisMapper.updateByPrimaryKey(projectAnalysis);
			}else{
				ProjectAnalysisMapper.insertSelective(projectAnalysis);
			}
		}
	}

	@Override
	public List getProjectAllmonth() {
		return ProjectAnalysisMapper.getProjectAllmonth();
	}

}
